package com.hua.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Read_utils {
    public static List<String> read_file(String category) throws IOException {
        List<String> list = new ArrayList<>();
        File file2 = new File("/usr/local/data/" + category);
        File[] files = file2.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = new File(files[i].getAbsolutePath());
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            list.add(sb.toString());
        }
        return list;
    }

    public static List<String> read_lifetime() throws IOException {
        List<String> str = new ArrayList<>();
        File file = new File("/usr/local/data/average_life_expectancy.txt");
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                str.add(tempString);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return str;
    }




}
